package com.douyu.water.core.service;

import java.util.ArrayList;
import java.util.List;


import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.douyu.water.core.PagePara;
import com.douyu.water.core.dao.AreaMapper;
import com.douyu.water.core.model.Area;
import com.douyu.water.core.model.AreaExample;
import com.douyu.water.core.model.UserArea;

@Service("areaService")
public class AreaServiceImpl implements AreaService {

	@Autowired
	private AreaMapper areaMapper;
	
	@Override
	public List<Area> select(Area record, PagePara page) {
		List<Area> list = null;
		AreaExample example = createExample(record);
		if(page != null)
		{
			int total = areaMapper.countByExample(example);
			page.setTotal(total);
			example.setPagePara(page);
		}
		list = areaMapper.selectByExample(example);
		return list;
	}

	@Override
	public int insert(Area record) {
		int flag =  0;
		if(!checkArea(record))
		{
			return flag;
		}
		if(checkSame(record))
		{
			return 2;
		}
		flag = areaMapper.insert(record);
		return flag;
	}

	@Override
	public int update(Area record) {
		int flag = 0;
		if(!checkArea(record))
		{
			return flag;
		}
		if(checkSame(record))
		{
			return 2;
		}
		flag = areaMapper.updateByPrimaryKey(record);
		return flag;
	}

	@Override
	public Area view(Area record) {
		Area area = null;
		if(!checkArea(record))
		{
			return area;
		}
		area = areaMapper.selectByPrimaryKey(record.getUcode().trim());
		return area;
	}

	@Override
	public int delete(Area record) {
		int flag = 0;
		if(!checkArea(record))
		{
			return flag;
		}
		flag = areaMapper.deleteByPrimaryKey(record.getUcode().trim());
		return flag;
	}
	
	@Override
	public List<Area> selectParent(Area area)
	{
		List<Area> list = null;
		AreaExample example = new AreaExample();
		AreaExample.Criteria criteria = example.createCriteria();
		if(area != null && area.getUcode() != null)
		{
			criteria.andParentEqualTo(area.getUcode().trim());
		}
		else if(area!= null && area.getUcode() == null)
		{
			criteria.andParentIsNull();
		}
		list = areaMapper.selectByExample(example);
		return list;
	}
	
	@Override
	public List<Area> selectAuth(Area area)
	{
		List<Area> list = null;
		list = this.select(area, null);
		//检索用户已经分配的地区
		if(!StringUtils.isBlank(area.getUserCode()))
		{
			List<UserArea> ualist = areaMapper.selectUserArea(area.getUserCode().trim());
			processArea(list,ualist);
		}
		return list;
	}
	
	/**
	 * 处理地区
	 * @param list
	 * @param ualist
	 */
	private void processArea(List<Area> list,List<UserArea> ualist)
	{
		if(list == null || ualist == null
				|| list.size() == 0 || ualist.size() == 0)
		{
			return;
		}
		//获取已经分配的
		List<String> auth = new ArrayList<String>();
		for(UserArea ua : ualist)
		{
			String area = ua.getAreaCode();
			if(!StringUtils.isBlank(area))
			{
				auth.add(area.trim());
			}
		}
		//处理行政区划列表
		for(Area a : list)
		{
			String code = a.getUcode();
			if(auth.contains(code.trim()))
			{
				a.setChecked(true);
			}
		}
	}
	/**
	 * 检查行政区划是否
	 * @param record
	 * @return
	 */
	private boolean checkArea(Area record)
	{
		boolean flag = false;
		if(record == null || StringUtils.isBlank(record.getUcode()))
		{
			return flag;
		}
		flag = true;
		return flag;
	}

	@Override
	public int insertAuth(UserArea ua)
	{
		int flag = 0;
		if(ua == null || StringUtils.isBlank(ua.getUserCode()) || StringUtils.isBlank(ua.getAreaCode()))
		{
			return flag;
		}
		List<UserArea> list = createUAList(ua);
		if(list != null && list.size() > 0)
		{
			//int temp = areaMapper.deleteUserArea(ua.getUserCode().trim());
			flag = areaMapper.insertAuth(list);
		}
		return flag;
	}
	
	private List<UserArea> createUAList(UserArea ua)
	{
		List<UserArea> list = new ArrayList<UserArea>();
		if(ua == null || StringUtils.isBlank(ua.getUserCode()) || StringUtils.isBlank(ua.getAreaCode()))
		{
			return null;
		}
		String userCode = ua.getUserCode();
		String areas = ua.getAreaCode();
		String[] areaCode = areas.split("[, ]+");
		for(String acode : areaCode)
		{
			if(!StringUtils.isBlank(acode))
			{
				list.add(new UserArea(userCode.trim(),acode.trim()));
			}
		}
		return list;
	}
	/**
	 * 检查行政区划名称是否重复
	 * @param record Area
	 * @return boolean
	 */
	private boolean checkSame(Area record)
	{
		boolean flag = false;
		if(record == null || StringUtils.isBlank(record.getUname()))
		{
			return flag;
		}
		//形成检索条件
		AreaExample example = new AreaExample();
		AreaExample.Criteria criteria = example.createCriteria();
		if(record.getParent() == null)
		{
			criteria.andParentIsNull();
		}
		else
		{
			criteria.andParentEqualTo(record.getParent().trim());
		}
		if(record.getLev() != null)
		{
			criteria.andLevEqualTo(record.getLev());
		}
		if(!StringUtils.isBlank(record.getUcode()))
		{
			criteria.andUcodeNotEqualTo(record.getUcode().trim());
		}
		criteria.andUnameEqualTo(record.getUname().trim());
		//检索符合条件的记录数
		int cnt = areaMapper.countByExample(example);
		if(cnt > 0)
		{
			flag = true;
		}
		return flag;
	}
	/**
	 * 创建检索条件对象
	 * @param record Area
	 * @return AreaExample
	 */
	private AreaExample createExample(Area record)
	{
		AreaExample example = new AreaExample();
		AreaExample.Criteria criteria = example.createCriteria();
		criteria.andUcodeIsNotNull();
		if(record != null)
		{
			if(!StringUtils.isBlank(record.getUcode()))
			{
				criteria.andUcodeEqualTo(record.getUcode().trim());
			}
			if(!StringUtils.isBlank(record.getUname()))
			{
				criteria.andUnameLike("%" + record.getUname().trim() + "%");
			}
			if(!StringUtils.isBlank(record.getParent()))
			{
				criteria.andParentEqualTo(record.getParent().trim());
			}
			if(record.getLev() != null)
			{
				criteria.andLevEqualTo(record.getLev());
			}
		}
		example.setOrderByClause(" lev asc,ucode asc");
		return example;
	}
}
